package com.beyond.pm.ui.home

import android.annotation.SuppressLint
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PageSize
import androidx.compose.foundation.pager.PagerDefaults
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.FabPosition
import androidx.compose.material.FloatingActionButton
import androidx.compose.material.Icon
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Flare
import androidx.compose.material.rememberScaffoldState
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.hilt.navigation.compose.hiltViewModel
import com.beyond.pm.bean.RouteScreenParameter
import com.beyond.pm.component.BaseEventComponent
import com.beyond.pm.local.LocalNavController
import com.beyond.pm.local.AppNavHostController
import com.beyond.pm.ui.home.config.ConfigScreen
import com.beyond.pm.ui.home.drawer.AppDrawer
import com.beyond.pm.ui.home.feature.FeatureScreen
import com.beyond.pm.ui.home.graph.GraphScreen
import kotlinx.coroutines.launch

/**
 * @author chen
 * @time 2023/5/31 13:34
 * @name ProjectManage
 * @class describe
 */
@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterialApi::class)
@SuppressLint("RememberReturnType", "UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun HomeScreen(viewModel: HomeViewModel = hiltViewModel(),) {
    val pagerState =
        rememberPagerState(initialPage = 0, initialPageOffsetFraction = 0f, pageCount = { 3 })
    val coroutineScope = rememberCoroutineScope()

    val nav = LocalNavController.current
    val appNav = remember(nav) { AppNavHostController(nav) }

    val routeScreenParameters = listOf(
        RouteScreenParameter.Graph, RouteScreenParameter.Feature, RouteScreenParameter.Config
    )
    val scaffoldState = rememberScaffoldState()
    BaseEventComponent(viewModel = viewModel) {
    Scaffold(
        scaffoldState = scaffoldState,
        drawerContent = {
            AppDrawer(scaffoldState = scaffoldState){
                appNav.navigate(it)
            }
        },
        drawerGesturesEnabled = true,
        bottomBar = {
            NavigationBar {
                routeScreenParameters.forEachIndexed { index, item ->
                    val color =
                        animateColorAsState(if (pagerState.currentPage == index) MaterialTheme.colorScheme.primary else Color.Gray)
                    NavigationBarItem(
                        icon = {
                            Icon(
                                item.bean.icon,
                                contentDescription = stringResource(item.bean.labelId),
                                tint = color.value
                            )
                        },
                        label = {
                            Text(
                                stringResource(item.bean.labelId),
                                color = MaterialTheme.colorScheme.onBackground
                            )
                        },
                        selected = pagerState.currentPage == index,
                        onClick = {
                            coroutineScope.launch {
                                pagerState.scrollToPage(index)
                            }
                        }
                    )
                }
            }
//            BottomNavigation(backgroundColor = Color.White,
//                contentColor = Color.Black,
//                elevation = 8.dp,
//                content = {
//                    screens.forEachIndexed { index, screen ->
//                        BottomNavigationItem(icon = {
//                            Icon(
//                                screen.icon,
//                                contentDescription = null,
//                                tint = if (pagerState.currentPage == index) Color.Blue else Color.Gray
//                            )
//                        },
//                            label = { Text(screen.title) },
//                            selected = pagerState.currentPage == index,
//                            onClick = {
//                                coroutineScope.launch {
//                                    pagerState.scrollToPage(index)
//                                }
//                            })
//                    }
//                })
        },
        floatingActionButton = {
            FloatingActionButton(
                onClick = {
                    viewModel.dispatch(HomeUiIntent.Test("1212"))
                },
                content = { Icon(Icons.Default.Flare, contentDescription = null) },
            )
        },
        floatingActionButtonPosition = FabPosition.End,
        isFloatingActionButtonDocked = false,
        content = {
            HomeContent(it, pagerState) {
                coroutineScope.launch {
                    scaffoldState.drawerState.open()
                }
            }
        })
    }
}


@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun HomeContent(
    paddingValues: PaddingValues,
    pagerState: PagerState,
    openDrawer: () -> Unit,
) {

        HorizontalPager(modifier = Modifier
            .fillMaxSize()
            .padding(paddingValues),
            state = pagerState,
            userScrollEnabled = false,
            reverseLayout = false,
            pageSize = PageSize.Fill,
            flingBehavior = PagerDefaults.flingBehavior(state = pagerState),
            pageNestedScrollConnection = PagerDefaults.pageNestedScrollConnection(
                Orientation.Horizontal
            ),
            pageContent = {
                when (it) {
                    0 -> GraphScreen(pagerState.currentPage == 0, openDrawer)
                    1 -> FeatureScreen(pagerState.currentPage == 1, openDrawer)
                    2 -> ConfigScreen(pagerState.currentPage == 2, openDrawer)
                }
            })

}


